//
//  HealthReportCommentViewController.m
//  1gene
//
//  Created by 1jiyin on 16/5/26.
//  Copyright © 2016年 1jiyin. All rights reserved.
//

#import "HealthReportCommentViewController.h"

@interface HealthReportCommentViewController ()<UITextViewDelegate>

@property(nonatomic,copy)UITextView *myTextView;

@end

@implementation HealthReportCommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:[UIView new]];
    [self createNavigation];
    self.view.backgroundColor=[UIColor whiteColor];
    [self createUI];
    // Do any additional setup after loading the view.
}

-(void)createUI
{
    //评论
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(20, 64+15, self.view.frame.size.width-20*2, 270)];
    view.layer.borderColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1].CGColor;
    view.layer.borderWidth=2.0;
    view.layer.cornerRadius=7.0;
    view.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:view];
    
    
    UITextView *textView=[[UITextView alloc]initWithFrame:CGRectMake(10, 10, view.frame.size.width-10*2, view.frame.size.height-10*2)];
    textView.text=@"请填写评论...";
    textView.delegate=self;
    textView.textColor=[UIColor blackColor];
    textView.font=[UIFont systemFontOfSize:16];
    [view addSubview:textView];
    _myTextView=textView;
}

-(void)textViewDidBeginEditing:(UITextView *)textView
{
    
    if ([textView.text isEqualToString:@"请填写评论..."]) {
        textView.text=@"";
    }
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.navigationController.view endEditing:YES];
}


-(void)createNavigation
{
    self.navigationController.navigationBar.hidden=NO;
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    //返回
    UIButton *leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame=CGRectMake(0, 0, 25, 25);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"left_back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    
    //标题
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
    title.text=@"评价";
    title.font=[UIFont systemFontOfSize:22];
    title.textAlignment=NSTextAlignmentCenter;
    title.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=title;
    
    //发表
    UIButton *rightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.bounds=CGRectMake(0, 0, 50, 25);
    rightBtn.layer.borderColor=[UIColor whiteColor].CGColor;
    rightBtn.layer.borderWidth=1.0;
    rightBtn.layer.cornerRadius=5.0;
    rightBtn.backgroundColor=[UIColor whiteColor];
    rightBtn.titleLabel.font=[UIFont systemFontOfSize:16];
    [rightBtn setTitle:@"发表" forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setTitleColor:[UIColor colorWithRed:243/255.0 green:132/255.0 blue:0 alpha:1] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:rightBtn];
}

//发表评论
-(void)rightBtnClick:(UIButton *)sender
{
    if (_myTextView.text.length==0) {
        [self.view showTiShiMessage:@"内容不能为空"];
        return;
    }else if ([_myTextView.text isEqualToString:@"请填写评论..."]){
        [self.view showTiShiMessage:@"内容不能为空"];
        return;
    }
    [self makeCommentData:_myTextView.text];
}


-(void)makeCommentData:(NSString *)content
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token= [user objectForKey:@"token"];
    
    [self.view isLoading:YES];
    
    [manager POST:[NSString stringWithFormat:CommentTrade,token] parameters:@{@"content":content} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self.view isLoading:NO];
        [self.view showTiShiMessage:@"评价成功"];
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"评价订单===%@",dic);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.view isLoading:NO];
        [self.view showTiShiMessage:@"评价失败"];
    }];

}



#pragma mark 返回上一页
-(void)leftBtnClick:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated
{
    [self.navigationController.navigationBar setTranslucent:YES];
}

-(void)viewWillDisappear:(BOOL)animated
{
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
